﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WorkOrder.Model;
using WorkOrder.Business;
using System.Drawing;

namespace WorkOrder
{
    public partial class addChangeoverProblems : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Model_Table_Cut_Problems model;
            if (Session["UserID"] == null || Session["UserID"].ToString() == "")
            {
                Session.Clear();
                ClientScript.RegisterClientScriptBlock(this.GetType(), "windohref", "<script type='text/javascript'>alert('对不起，您还没有登录系统！');window.parent.parent.location.href='./login.aspx';</script>");
                return;
            }
            Business_Table_Cut_Problems B_T_C = new Business_Table_Cut_Problems();
            if (!Page.IsPostBack)
            {
                ddlCutProblem.Items.Clear();
                ddlCutProblem.DataTextField = "changeover_problems_type_name";
                ddlCutProblem.DataValueField = "changeover_problems_type_id";
                ddlCutProblem.DataSource = B_T_C.GetChangeoverProblemsType();
                ddlCutProblem.DataBind();
                ddlCutProblem.Items.Add(new ListItem("-- 请选择 --", "0"));
                ddlCutProblem.SelectedIndex = ddlCutProblem.Items.Count - 1;
                if (!string.IsNullOrEmpty(Request.Params["cid"]))
                {
                    hidChangeoverID.Value = Request.Params["cid"];
                }
            }
            if (Page.IsPostBack)
            {
                //参数a的值的意义：0保存
                if (Request.Params["a"] == "0")
                {
                    if (!string.IsNullOrEmpty(hidID.Value))//更新已修改的记录
                    {
                        if (!new Business_Table_Permission().HasModify(Session["UserID"].ToString(), "Table_Cut_Problems"))
                        {
                            lblMsg.ForeColor = Color.Red;
                            lblMsg.Text = "提示信息：您没有修改割接单问题的权限！";
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "windowhref", "<script type='text/javascript'>alert('对不起，您没有修改割接单问题的权限！');window.close();</script>");
                            return;
                        }
                        DateTime startTime = DateTime.Now; 
                        DateTime endTime=DateTime.Now;
                        try { startTime = Convert.ToDateTime(txtProStartTime.Value.Trim()); }
                        catch { 
                            lblMsg.ForeColor = Color.Red;
                            lblMsg.Text = "提示信息：问题产生时间格式错误！";
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "windoef", "<script type='text/javascript'>alert('对不起，问题产生时间格式错误！');window.close();</script>");
                            return;
                        }
                        if (txtProEndTime.Value.Trim()!="")
                        {
                            try { endTime = Convert.ToDateTime(txtProEndTime.Value.Trim()); }
                            catch { 
                                lblMsg.ForeColor = Color.Red;
                                lblMsg.Text = "提示信息：问题解决时间格式错误！";
                                ClientScript.RegisterClientScriptBlock(this.GetType(), "wndoef", "<script type='text/javascript'>alert('对不起，问题解决时间格式错误！');window.close();</script>");
                                return;
                            }
                        }
                        model = new Model_Table_Cut_Problems(hidID.Value, hidChangeoverID.Value, ddlCutProblem.SelectedValue, Server.HtmlEncode(txtCutDesc.Text.Trim()), txtProStartTime.Value.Trim(), txtProEndTime.Value.Trim());
                        if (B_T_C.Update(model))
                        {
                            hidProblem_start_time.Value = hidProblem_start_time != null ? txtProStartTime.Value.Trim() : "";
                            hidProblem_end_time.Value = hidProblem_end_time != null ? txtProEndTime.Value.Trim() : "";
                            hidDsc.Value = hidDsc != null ? txtCutDesc.ValidationGroup.Trim() : "";
                            HidProblem_type.Value = HidProblem_type != null ? ddlCutProblem.SelectedItem.Text : "";
                            hidID.Value = hidID != null ? "" : "";
                            txtCutDesc.Text = "";
                            txtProEndTime.Value = "";
                            txtProStartTime.Value = "";
                            ddlCutProblem.SelectedIndex = ddlCutProblem.Items.Count - 1;
                            txtCutDesc.Enabled = false;
                            txtProEndTime.Disabled = true;
                            txtProStartTime.Disabled = true;
                            ddlCutProblem.Enabled = false;
                            lblMsg.ForeColor = Color.DarkGreen;
                            lblMsg.Text = "提示信息：成功保存一条记录！";
                        }
                    }
                    else
                    {
                        if (!new Business_Table_Permission().HasInsert(Session["UserID"].ToString(), "Table_Cut_Problems"))
                        {
                            lblMsg.ForeColor = Color.Red;
                            lblMsg.Text = "提示信息：您没有新增割接单问题的权限！";
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "windowhref", "<script type='text/javascript'>alert('对不起，您没有新增割接单的权限！');window.close();</script>");
                            return;
                        }
                        DateTime startTime = DateTime.Now;
                        DateTime endTime = DateTime.Now;
                        try { startTime = Convert.ToDateTime(txtProStartTime.Value.Trim()); }
                        catch
                        {
                            lblMsg.ForeColor = Color.Red;
                            lblMsg.Text = "提示信息：问题产生时间格式错误！";
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "windoef", "<script type='text/javascript'>alert('对不起，问题产生时间格式错误！');window.close();</script>");
                            return;
                        }
                        if (txtProEndTime.Value.Trim() != "")
                        {
                            try { endTime = Convert.ToDateTime(txtProEndTime.Value.Trim()); }
                            catch
                            {
                                lblMsg.ForeColor = Color.Red;
                                lblMsg.Text = "提示信息：问题解决时间格式错误！";
                                ClientScript.RegisterClientScriptBlock(this.GetType(), "wndoef", "<script type='text/javascript'>alert('对不起，问题解决时间格式错误！');window.close();</script>");
                                return;
                            }
                        }
                        model = new Model_Table_Cut_Problems("0", hidChangeoverID.Value, ddlCutProblem.SelectedValue, Server.HtmlEncode(txtCutDesc.Text.Trim()), txtProStartTime.Value.Trim(), txtProEndTime.Value.Trim());
                        if (B_T_C.Insert(model))
                        {
                            hidProblem_start_time.Value = "";
                            hidProblem_end_time.Value = "";
                            hidDsc.Value = "";
                            hidID.Value = "";
                            HidProblem_type.Value = "";
                            //hidChangeoverID.Value = "";
                            txtProEndTime.Value = "";
                            txtProStartTime.Value = "";
                            ddlCutProblem.SelectedIndex = ddlCutProblem.Items.Count - 1;
                            txtCutDesc.Text = "";
                            lblMsg.ForeColor = Color.DarkGreen;
                            lblMsg.Text = "提示信息：成功保存一条记录！";
                        }
                    }
                }
            }
            //加载编辑的资料
            if (Request.Params["a"] == "1")
            {
                if (!String.IsNullOrEmpty(Request.Params["b"]))
                {
                    model = B_T_C.GetData(Request.Params["b"]);
                    if (model != null)
                    {
                        txtProStartTime.Value = model.Problem_start_time;
                        txtProEndTime.Value = model.Problem_end_time;
                        hidChangeoverID.Value=model.Changeover_id;
                        txtCutDesc.Text=Server.HtmlDecode(model.Cute_problem_desc);
                        ddlCutProblem.SelectedIndex = ddlCutProblem.Items.IndexOf(ddlCutProblem.Items.FindByValue(model.Cute_problem_type_id));
                        hidID.Value = model.Cute_problem_id;
                    }
                }
            }
        }
    }
}
